package com.local.seckill.controller;

import com.local.common.util.PageResultUtil;
import com.local.common.util.ResultUtil;
import com.local.seckill.pojo.Seckill;
import com.local.seckill.pojo.SeckillListRequestParam;
import com.local.seckill.service.SeckillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@Controller
@RequestMapping("/seckill")
public class SeckillController {

    @Autowired
    private SeckillService seckillService;

    @GetMapping("/list")
    @ResponseBody
    public ResultUtil list(@RequestBody SeckillListRequestParam requestParam) {
        Integer page = requestParam.getPage();
        Integer limit = requestParam.getLimit();
        String sort = requestParam.getSort();
        String order = requestParam.getOrder();
        Integer goodsId = requestParam.getGoodsId();
        Integer seckillStatus = requestParam.getSeckillStatus();
        String startTime = requestParam.getStartTime();
        String endTime = requestParam.getEndTime();
        Integer isDeleted = requestParam.getIsDeleted();

        page = Objects.isNull(page) || page <= 0 ? 1 : page;
        limit = Objects.isNull(limit) || limit <= 0 ? 1 : limit;
        sort = Objects.isNull(sort) || sort.isEmpty() ? "seckill_id" : sort;
        order = Objects.isNull(order) || order.isEmpty() ? "desc" : order;

        PageResultUtil<Seckill> seckillList = seckillService.findSeckillList(page, limit, sort, order, goodsId, seckillStatus, startTime, endTime, isDeleted);
        if (Objects.nonNull(seckillList)) {
            return new ResultUtil(200, "", seckillList);
        } else {
            return new ResultUtil(201, "请求失败，请稍后再试");
        }
    }


    /**
     * 根据id查找数据
     * @param seckillId
     * @return
     */
    @GetMapping("/{seckillId}")
    @ResponseBody
    public ResultUtil getById(@PathVariable("seckillId") Integer seckillId) {
        Seckill seckill = seckillService.selectById(seckillId);
        if (Objects.nonNull(seckill)) {
            return new ResultUtil(200, "", seckill);
        } else {
            return new ResultUtil(201, "请求失败，请稍后再试");
        }
    }


    /**
     * 新增
     * @param seckill
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    public ResultUtil save(@RequestBody Seckill seckill) {
        Integer saveResult = seckillService.save(seckill);
        if (saveResult > 0) {
            return new ResultUtil(200, "新增成功");
        } else {
            return new ResultUtil(201, "新增失败，请稍后再试");
        }
    }


    /**
     * 修改
     * @param seckill
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    public ResultUtil update(@RequestBody Seckill seckill) {
        Integer updateResult = seckillService.update(seckill);
        if (updateResult > 0) {
            return new ResultUtil(200, "修改成功");
        } else {
            return new ResultUtil(201, "修改失败，请稍后再试");
        }
    }


    /**
     * 根据id逻辑删除
     * @param seckillId
     * @return
     */
    @DeleteMapping("/{seckillId}")
    @ResponseBody
    public ResultUtil delete(@PathVariable("seckillId") Integer seckillId) {
        Integer deleteResult = seckillService.delete(seckillId);
        if (deleteResult > 0) {
            return new ResultUtil(200, "删除成功");
        } else {
            return new ResultUtil(201, "删除失败，请稍后再试");
        }
    }
}


